package com.vast.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vast.admin.entity.ContractDetail;
import com.vast.admin.entity.Templete;
import com.vast.admin.mapper.ContractDetailMapper;
import com.vast.admin.mapper.TempleteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class ContractDetailService extends ServiceImpl<ContractDetailMapper, ContractDetail> {

    @Autowired
    private TempleteMapper templeteMapper;

    /**
     * 通过合同id获取合同详情
     * @param contractId
     * @return
     */
    public List<ContractDetail> getByContractId(Long contractId) {
        QueryWrapper<ContractDetail> contractDetailQueryWrapper = new QueryWrapper<>();
        contractDetailQueryWrapper.eq(ContractDetail.COL_CONTRACT_ID, contractId).orderByDesc(ContractDetail.COL_CREATE_TIME);
        List<ContractDetail> contractDetailList = list(contractDetailQueryWrapper);
        if (!contractDetailList.isEmpty()){
            for (ContractDetail contractDetail : contractDetailList) {
                if(contractDetail.getTempleteId() != null){
                   Templete templete = templeteMapper.selectOne(new QueryWrapper<Templete>().eq(Templete.COL_ID,contractDetail.getTempleteId()).in(Templete.COL_VALID,0,1));
                   if(templete != null){
                       contractDetail.setTemplateName(templete.getTempleteName());
                   }
                }
            }
        }
        return contractDetailList;
    }

    /**
     * 上上签合同id获取详情
     * @param bestsignContractId
     * @return
     */
    public List<ContractDetail> getByBestsignContractId(String bestsignContractId) {
        QueryWrapper<ContractDetail> contractDetailQueryWrapper = new QueryWrapper<>();
        contractDetailQueryWrapper.eq(ContractDetail.COL_BESTSIGN_CONTRACT_ID, bestsignContractId);
        return super.list(contractDetailQueryWrapper);
    }

    /**
     * 获取未签署完成合同详情的数量
     * @param contractId
     * @return
     */
    public Integer getNotSignContractCount(Long contractId) {
        QueryWrapper<ContractDetail> contractDetailQueryWrapper = new QueryWrapper<>();
        contractDetailQueryWrapper.eq(ContractDetail.COL_CONTRACT_ID, contractId)
                .ne(ContractDetail.COL_STATUS, 3);
        return super.count(contractDetailQueryWrapper);
    }

    /**
     * 通过合同主流程id修改合同详情状态
     * @param status
     * @param contractId
     */
    public void changeStatusByContractId(Integer status, Long contractId, Long updateManagerId) {
        UpdateWrapper<ContractDetail> contractDetailUpdateWrapper = new UpdateWrapper<>();
        contractDetailUpdateWrapper.set(ContractDetail.COL_STATUS, status)
                .set(ContractDetail.COL_UPDATE_TIME, new Date())
                .set(ContractDetail.COL_UPDATE_BY, updateManagerId);
        contractDetailUpdateWrapper.eq(ContractDetail.COL_CONTRACT_ID, contractId);
        super.update(contractDetailUpdateWrapper);
    }

}
